package org.testfire.agent.tags;

import java.io.IOException;
import java.io.File;
import javax.servlet.ServletRequest;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.PageContext;
import javax.servlet.jsp.tagext.SimpleTagSupport;
import org.testfire.agent.runner.TestSetRunner;
import org.testfire.model.TestsetType;
import org.testfire.model.exception.CannotParseObjectException;
import org.testfire.support.StringOutputStream;
import org.testfire.util.DOM3XmlReader;
import org.testfire.util.DomReader;
import org.w3c.dom.Document;
import org.w3c.dom.Element;

/**
 * Created by IntelliJ IDEA.
 * User: granum
 * Date: Sep 30, 2007
 * Time: 12:54:13 PM
 *
 */
public class TestsetRunnerTag extends SimpleTagSupport
{
    public static final String BY_JAVACODE = "javacode";

    public static final String BY_XML = "xml";

    private String by = "xml";

    public void doTag() throws JspException, IOException
    {
        ServletRequest request = ((PageContext)super.getJspContext()).getRequest();

        if ( by.equals( BY_JAVACODE ) )
        {
            this.doByJavaCode( request );
        }
        else if ( by.equals( BY_XML ) )
        {
            this.doByXml( request );
        }

    }

    public void doByJavaCode( ServletRequest request )
    {
//        TestSetRunner runner = null;
//        String testsetName = request.getParameter( "testset_name" );
//        String testsetPackage = request.getParameter( "testset_package" );
//        String testsetId = request.getParameter( "testset_id" );
//        String testsetResultId = request.getParameter( "testsetResult_id" );
//        String testsetJavaCode = request.getParameter( "testset_javacode" );
//
//        if ( testsetId != null )
//        {
//            runner = new TestSetRunner( testsetName, testsetPackage, testsetId, testsetResultId, testsetJavaCode );
//        }
//        else
//        {
//            try
//            {
//                runner = new TestSetRunner( testsetName, testsetPackage, testsetJavaCode );
//
//            }
//            catch ( IOException e )
//            {
//                outputBadRequestResponse();
//                return;
//            }
//        }
//        runner.run();
//        outputRunnerResults( runner );

    }

    public void doByXml( ServletRequest request )
    {
//        String value = request.getParameter( "testset_xml" );
//        // parse the document into a testset.
//        Document doc = null;
//        if ( value == null )
//        {
//            outputBadRequestResponse();
//            return;
//        }
//        try
//        {
//            String[] schema = {"http://localhost:9292/schema/base.xsd"};
//            File f = new File( System.getProperty( "user.dir" ) );
//            f = new File( f, "resources/tomcat/webapps/ROOT/schema/base.xsd" );
//            DOM3XmlReader.SchemaResolver[] resolvers = {
//                new DOM3XmlReader.SchemaResolver( "http://gettestfire.org/schema/base.xsd", f )};
//            doc = DOM3XmlReader.getDocument( DomReader.createTempFileForXMLString( value ), schema, resolvers );
//        }
//        catch ( Exception e )
//        {
//            e.printStackTrace();
//            return;
//        }
//        Element de = doc.getDocumentElement();
//        TestsetType testset;
//        try
//        {
//            testset = new TestsetType( de );
//        }
//        catch ( CannotParseObjectException e )
//        {
//            e.printStackTrace();
//            return;
//        }
//        String testsetJavaCode = (String)testset.translate( testset );
//        String testsetName = testset.getName();
//        String testsetPackage = testset.getClassPackage();
//        TestSetRunner runner;
//        try
//        {
//            runner = new TestSetRunner( testsetName, testsetPackage, testsetJavaCode );
//            runner.run();
//            outputRunnerResults( runner );
//
//        }
//        catch ( IOException e )
//        {
//            outputBadRequestResponse();
//        }
//

    }

    public void outputRunnerResults( TestSetRunner runner )
    {
//        try
//        {
//            JspWriter out = super.getJspContext().getOut();
//            out.println( "<h2>Results for testset '" + runner.getTestsetName() + "'</h2>" );
//            if ( runner.getError() != null )
//            {
//                out.println( "<h3>Error Compiling or Running:</h3><p>" );
//                out.print( StringOutputStream.printException( runner.getError() ) );
//                out.print( "</p>" );
//            }
//            else if ( runner.getFailures().size() > 0 )
//            {
//
//            }
//            else
//            {
//                out.println(
//                    String.format( "<h3>%1$d tests ran with no failures.</h3>", runner.getResult().getRunCount() ) );
//                out.println(
//                    String.format( "<h3>%1$d tests were ignored.</h3>", runner.getResult().getIgnoreCount() ) );
//                out.println( "<h6>Total Run Time: " + runner.getResult().getRunTime() + " milliseconds</h6>" );
//            }
//        }
//        catch ( IOException e )
//        {
//            e.printStackTrace();
//        }

    }

    public void outputBadRequestResponse()
    {
        JspWriter out = super.getJspContext().getOut();
        try
        {
            out.println( "<h2>Could not process request.</h2>" );
        }
        catch ( IOException e )
        {
            e.printStackTrace();
        }
    }

    public String getBy()
    {
        return by;
    }

    public void setBy( String by )
    {
        this.by = by;
    }

}
